package webengine

import (
	"net/http"
	"os"
	"time"

	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	gs "github.com/swaggo/gin-swagger"

	"gin-api-server/core_library/global"
	_ "gin-api-server/docs"
)

func InitSystemroute(g *gin.Engine) {
	// 健康检测
	g.GET("/system.state", func(c *gin.Context) {

		pid := os.Getpid()
		port := global.Config.Server.ListenPort
		c.HTML(http.StatusOK, "index.html", gin.H{
			"time": time.Now().Format("2006/01/02 15:04"),
			"pid":  pid,
			"port": port,
		})
	})

	g.GET("/routes", func(context *gin.Context) {
		routes := g.Routes()
		var rs []map[string]any
		// keyName := "RecodeApiRequestNum"
		for _, route := range routes {
			// s := fmt.Sprintf("%s-%s", route.Method, route.Path)
			rs = append(rs, map[string]any{
				"method": route.Method,
				"url":    route.Path,
				"num":    0,
			})
		}

		context.HTML(http.StatusOK, "routes.html", gin.H{
			"routes": rs,
		})
	})

	g.GET("/swagger/*any", gs.WrapHandler(swaggerFiles.Handler))

}
